
package com.dps.omc.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dps.omc.model.TemplateShell;
import com.dps.omc.model.TemplateType;
import com.dps.omc.service.IHandlerService;
import com.dps.omc.service.ITemplateHandlerService;
import com.dps.omc.service.ITemplateTypeService;
import com.dps.omc.model.TemplateHandler;
import com.dps.omc.model.User;
import com.dps.omc.service.ITemplateShellService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dps.common.domain.view.BizData4Page;
import com.dps.common.utils.IdGenerator;
import com.dps.common.utils.JsonResult;
import com.dps.common.utils.PageParam;
import com.dps.common.utils.Constants;

@Controller
public class TemplateShellAction extends AbstractAdminController<ITemplateShellService>{

    @Autowired
    private ITemplateShellService templateShellService;
    @Autowired
    private ITemplateTypeService templateTypeService;
    @Autowired
    private ITemplateHandlerService templateHandlerService;
    @Autowired
    private IHandlerService handlerService;

    @RequestMapping(value="/templateshell/tree")
    public String tree(String paramId, TemplateShell templateShell, ModelMap model, PageParam pageParam, HttpServletRequest request, HttpServletResponse response){

        //获取参数
        Map<String, Object> conditions = getQueryMap(templateShell);
        BizData4Page<TemplateShell> pageCtx = doPage(model, conditions, pageParam);
        model.addAttribute("templateShell", templateShell);
        model.addAttribute("typeTree", templateTypeService.getTypeTree());
        model.addAttribute("paramId", paramId);
        return "module/templateshell/tree";
    }

    @RequestMapping(value="/templateshell/list")
    public String list(TemplateShell templateShell,ModelMap model,PageParam pageParam,HttpServletRequest request,HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(templateShell); 	
    	BizData4Page<TemplateShell> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("templateShell", templateShell);
    	return "/module/templateshell/list";
    }    
    
    @RequestMapping(value="/templateshell/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

        List<TemplateType> templateTypes = templateTypeService.findAll();
        model.addAttribute("templateTypes", templateTypes);
    	model.addAttribute("templateShell", new TemplateShell());
        model.addAttribute("typeTree", templateTypeService.getTypeTree());
        model.addAttribute("handlers", handlerService.findAll());
        return "module/templateshell/edit";
    }    
    
    @RequestMapping(value="/templateshell/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	TemplateShell templateShell = templateShellService.fetch(id);
        List<TemplateType> templateTypes = templateTypeService.findAll();
        List<TemplateHandler> templateHandlers = templateHandlerService.findList("templateId",id);
        model.addAttribute("templateTypes", templateTypes);
    	model.addAttribute("templateShell", templateShell);
        model.addAttribute("typeTree", templateTypeService.getTypeTree());
        model.addAttribute("handlers", handlerService.findAll());
        model.addAttribute("templateHandlers",templateHandlers);
        return "module/templateshell/edit";
    }    

//    private TreeNode getTypeTree()
//    {
//        List<TemplateType> types = templateTypeService.findAll();
//
//        List<TreeNode> treeNodes = new ArrayList<>();
//        for(TemplateType type:types)
//        {
//            TreeNode node = new TreeNode();
//            node.setId(type.getId());
//            node.setName(type.getTypeName());
//            node.setPid(type.getPid());
//            treeNodes.add(node);
//        }
//        TreeNode typeTree = TreeBuilder.buildTree(treeNodes);
//        return typeTree;
//    }

    @RequestMapping(value="/templateshell/save")
    @ResponseBody
    public String save(HttpServletRequest request,TemplateShell templateShell){
        String[] handlers = request.getParameterValues("handler");
		boolean isAddNew = StringUtils.isBlank(templateShell.getId())?true:false;
        User user=getCurrentUser();
		if(isAddNew)
		{
			templateShell.setId(IdGenerator.createNewId());
            templateShell.setCreaterId(user.getId());
            templateShell.setCreaterName(user.getRealName());
			templateShellService.add(templateShell);
            this.createTemplateHandlers(handlers,templateShell.getId());
		}
		else
		{
			templateShellService.update(templateShell);
            this.createTemplateHandlers(handlers,templateShell.getId());
        }
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }

    private void createTemplateHandlers(String[] handlers,String templateId)
    {
        if(handlers!=null&&handlers.length>0)
        {
            templateHandlerService.deleteByProperty("templateId",templateId);
            int index = 0;
            for(String handler:handlers)
            {
                index++;
                int seq = index*10;
                TemplateHandler templateHandler = new TemplateHandler();
                templateHandler.setId(IdGenerator.createNewId());
                templateHandler.setClazz(handler);
                templateHandler.setTemplateId(templateId);
                templateHandler.setSeq(seq);
                templateHandlerService.add(templateHandler);
            }
        }

    }
    
    @RequestMapping(value="/templateshell/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	templateShellService.deleteByIds(ids);
//    	return JsonResult.DELETE_SUCCESS.toString();
        return JsonResult.deleteSuccess(Constants.MAIN_TAB).toString();
    }

    @RequestMapping(value="/templateshell/lookup")
    public String lookUp(String paramId,TemplateShell templateShell,ModelMap model,PageParam pageParam,HttpServletRequest request,HttpServletResponse response){

        //获取参数
        Map<String, Object> conditions = getQueryMap(templateShell);
        BizData4Page<TemplateShell> pageCtx = doPage(model, conditions, pageParam);
        model.addAttribute("templateShell", templateShell);
        model.addAttribute("typeTree", templateTypeService.getTypeTree());
        model.addAttribute("paramId", paramId);
        return "module/templateshell/lookup";
    }

    @RequestMapping(value="/templateshell/lookuplist")
    public String lookUpList(TemplateShell templateShell,ModelMap model,PageParam pageParam,HttpServletRequest request,HttpServletResponse response){

        Map<String, Object> conditions = getQueryMap(templateShell);
        BizData4Page<TemplateShell> pageCtx = doPage(model, conditions, pageParam);
        model.addAttribute("templateShell", templateShell);
        return "module/templateshell/include/lookup_list";
    }

//    @RequestMapping(value="/templateshell/preview")
//    @ResponseBody
//    public String preview(String id,HttpServletRequest request) throws Exception {
//
//        TemplateShell templateShell = templateShellService.fetch(id);
//        return templateShell.getContent();
//    }

    @RequestMapping(value="/templateshell/preview")
    public String preview(String id,HttpServletRequest request,ModelMap model) throws Exception {

        TemplateShell templateShell = templateShellService.fetch(id);
        String content =  templateShell.getContent();
        if(StringUtils.isNotBlank(content))
        {
//            content = content.replaceAll("\n","<br/>");//命令换行
            content = content.replaceAll("<","&lt;");
            content = content.replaceAll(">","&gt;");
            content = content.replaceAll("\n","<br/>");//日志换行
        }
        model.addAttribute("content",content);
        return "module/templateshell/preview";
    }

    protected Map getQueryMap(TemplateShell templateShell)
    {
        Map<String, Object> conditions = new HashMap();

        conditions.put("name", templateShell.getName());
        conditions.put("content", templateShell.getContent());
        conditions.put("typeId", templateShell.getTypeId());
        conditions.put("typeName", templateShell.getTypeName());
        conditions.put("createrId", templateShell.getCreaterId());
        conditions.put("createrName", templateShell.getCreaterName());
        conditions.put("createDate", templateShell.getCreateDate());
        conditions.put("updaterId", templateShell.getUpdaterId());
        conditions.put("updateDate", templateShell.getUpdateDate());
        conditions.put("updaterName", templateShell.getUpdaterName());
        return conditions;
    }

    @Override
    protected ITemplateShellService getMainService() {
        return templateShellService;
    }

    @Override
    protected String getMainObjName() {
        return "templateshell";
    }

    @Override
    protected String getViewTitle() {
        return "templateshell";
    }
}
